package com.digitalchina.lxnsy.toolkit;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.res.Resources;
import android.os.Handler;
import android.os.Message;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.recyclerview.widget.LinearLayoutManager;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;
import com.digitalchina.lxnsy.R;
import com.digitalchina.lxnsy.activity.old.MyPrizeListActivity;
import com.digitalchina.lxnsy.base.BaseAdapter;
import com.digitalchina.lxnsy.constant.MSG;
import com.digitalchina.lxnsy.entity.CancelReasonVO;
import com.digitalchina.lxnsy.viewholder.OrderCancelReasonHolder;
import com.jude.easyrecyclerview.EasyRecyclerView;
import com.jude.easyrecyclerview.adapter.BaseViewHolder;
import com.jude.easyrecyclerview.adapter.RecyclerArrayAdapter;

import java.util.ArrayList;
import java.util.List;


/**
 * ================================================
 * 项目名称：杨凌农业大数据(数据采集)
 * 类描述：
 * 包名：com.digitalchina.bigdata.view
 * 创建人：郑皓
 * 创建时间：2016/4/12 16:35
 * 修改备注：
 * ================================================
 */
public class CustomDialog {


    /**
     * 原生单按钮 点击关闭提示框
     *
     * @param activity
     * @param strMessage
     */
    public static void nativeDialog(Activity activity, String strMessage) {
        new MaterialDialog.Builder(activity)
                .title("提示")
                .content(strMessage)
                .positiveText("确定")
                .show();
    }

    public static void nativeDialog(Context context, String strMessage) {
        new MaterialDialog.Builder(context)
                .title("提示")
                .content(strMessage)
                .positiveText("确定")
                .show();
    }


    /**
     * 原生单按钮 点击关闭当前页面
     *
     * @param activity
     * @param strMessage
     */
    public static void nativeDialogFinish(final Activity activity, String strMessage) {
        new MaterialDialog.Builder(activity)
                .title("提示")
                .content(strMessage)
                .positiveText("确定")
                .onPositive(new MaterialDialog.SingleButtonCallback() {
                    @Override
                    public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                        activity.finish();
                    }
                })
                .show();
    }



    /**
     * 加载框
     *
     * @param activity
     * @param strMessage
     * @return
     */
    public static ProgressDialog progressDialog(Activity activity, String strMessage) {
        ProgressDialog dialog = new ProgressDialog(activity);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setCanceledOnTouchOutside(false);
        dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        dialog.setMessage(strMessage);
        return dialog;
    }

    public static ProgressDialog progressDialog(Activity activity) {
        ProgressDialog dialog = new ProgressDialog(activity);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setCanceledOnTouchOutside(false);
        dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        return dialog;
    }

    /**
     * List 弹出选择框
     *
     * @param activity
     * @param title
     * @param data      数据源
     * @param loWhat    来源标识
     * @param poHandler
     */
    public static void listDialog(Activity activity, String title, final CharSequence[] data, final int loWhat, final Handler poHandler) {

        new AlertDialog.Builder(activity)
                .setTitle(title)
                .setIcon(R.mipmap.ic_launcher)
                .setItems(data, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        dialogInterface.dismiss();
                        if (poHandler != null) {
                            Message loMsg = new Message();
                            loMsg.what = loWhat;
                            loMsg.arg1 = i;
                            poHandler.sendMessage(loMsg);
                        }
                    }
                })
                .setNegativeButton("取消", null)
                .show();

    }


    public static void customListDialog(final Activity activity, String title, BaseAdapter adapter, final int loWhat, final Handler poHandler) {

        new AlertDialog.Builder(activity)
                .setTitle(title)
                .setIcon(R.mipmap.ic_launcher)
                .setAdapter(adapter, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        ToastUtil.showToast(activity, i);
                    }
                });

    }

    /**
     * 订单取消理由选择
     *
     * @param poActivity
     * @param poHandler
     */
    private static int mPosition;

    public static void orderCancelReasonDialog(final Activity poActivity, final Handler poHandler) {

        final RecyclerArrayAdapter adapter;

        final android.app.AlertDialog loDialog = new android.app.AlertDialog.Builder(poActivity)
                .create();
        LayoutInflater loInflater = poActivity.getLayoutInflater();
        loDialog.setView(loInflater.inflate(
                R.layout.dialog_cancel_reason_selector, null));
        loDialog.show();
        loDialog.getWindow().setGravity(Gravity.CENTER);
        loDialog.getWindow().setLayout(RelativeLayout.LayoutParams.MATCH_PARENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);
        loDialog.getWindow().setContentView(
                R.layout.dialog_cancel_reason_selector);
        WindowManager.LayoutParams lp = loDialog.getWindow().getAttributes();
        loDialog.getWindow().setGravity(Gravity.LEFT | Gravity.BOTTOM);
        loDialog.getWindow().setAttributes(lp);
        loDialog.setCanceledOnTouchOutside(true);
        loDialog.setCancelable(true);

        TextView submit = (TextView) loDialog
                .findViewById(R.id.dialog_tv_submit);
        EasyRecyclerView dialogRecyclerView = (EasyRecyclerView) loDialog
                .findViewById(R.id.dialog_recycler_view);
        ImageView dCancel = (ImageView) loDialog
                .findViewById(R.id.dialog_iv_cancel);

        dialogRecyclerView.setLayoutManager(new LinearLayoutManager(poActivity));
        dialogRecyclerView.addItemDecoration(Utils.setSpaceDecoration(poActivity, false, true, false));
        dialogRecyclerView.setAdapter(adapter = new RecyclerArrayAdapter(poActivity) {
            @Override
            public BaseViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) {
                return new OrderCancelReasonHolder(parent);
            }

        });

        adapter.addAll(getCancelReason(poActivity));

        adapter.setOnItemClickListener(new RecyclerArrayAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {

                mPosition = position;

                List<CancelReasonVO> cancelReasonVOList = adapter.getAllData();
                for (int i = 0; i < cancelReasonVOList.size(); i++) {
                    if (i == position)
                        cancelReasonVOList.get(i).setSelected(true);
                    else
                        cancelReasonVOList.get(i).setSelected(false);
                }
                adapter.notifyDataSetChanged();
            }
        });

        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                loDialog.dismiss();
                if (poHandler != null) {
                    Message loMsg = new Message();
                    loMsg.what = MSG.MSG_SELECT_ORDER_CANCEL_REASON_TAG;
                    loMsg.obj = adapter.getAllData().get(mPosition);
                    poHandler.sendMessage(loMsg);
                }
            }
        });

        dCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                loDialog.dismiss();
            }
        });
    }

    /**
     * 获取取消订单原因列表
     *
     * @return 取消订单原因集合
     */
    private static List<CancelReasonVO> getCancelReason(Activity poActivity) {
        List<CancelReasonVO> reasons = new ArrayList<>();
        Resources res = poActivity.getResources();
        String[] reason = res.getStringArray(R.array.cancelReason);
        for (int i = 0; i < reason.length; i++) {
            CancelReasonVO reasonVO = new CancelReasonVO();
            reasonVO.setCancelReason(reason[i]);
            reasonVO.setSelected(false);
            reasons.add(reasonVO);
        }
        return reasons;
    }


    /**
     * 抽奖弹出框
     *
     * @param poActivity
     * @param showType   0-已中奖   1-未中奖  2-无抽奖次数
     * @param prizeName
     */
    public static void lotteryDialog(final Activity poActivity, final int showType, String prizeName) {

        final android.app.AlertDialog loDialog = new android.app.AlertDialog.Builder(poActivity)
                .create();
        LayoutInflater loInflater = poActivity.getLayoutInflater();
        loDialog.setView(loInflater.inflate(
                R.layout.dialog_lottery, null));
        loDialog.show();
        loDialog.getWindow().setGravity(Gravity.CENTER);
        loDialog.getWindow().setLayout(RelativeLayout.LayoutParams.MATCH_PARENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);
        loDialog.getWindow().setBackgroundDrawableResource(R.color.transparent);
        loDialog.getWindow().setContentView(
                R.layout.dialog_lottery);
        loDialog.setCanceledOnTouchOutside(false);
        loDialog.setCancelable(false);

        ImageView ivTitle =  loDialog.findViewById(R.id.dialog_lottery_iv_title);
        ImageView ivBackground =  loDialog.findViewById(R.id.dialog_lottery_iv_background);
        TextView tvPrize =  loDialog.findViewById(R.id.dialog_lottery_tv_prize_name);
        TextView tvLeft =  loDialog.findViewById(R.id.dialog_lottery_tv_button_left);
        TextView tvRight =  loDialog.findViewById(R.id.dialog_lottery_tv_button_right);


        switch (showType) {
            case 0: // 用户中奖
                ivTitle.setImageResource(R.drawable.bg_lottery_congratulation_title);
                ivBackground.setImageResource(R.drawable.bg_lottery_congratulation);
                tvPrize.setText("恭喜抽中：\n" + prizeName);
                tvLeft.setText("继续抽奖");
                tvRight.setText("查看奖品");
                break;
            case 1: // 用户未中奖
                ivTitle.setImageResource(R.drawable.bg_lottery_regret_title);
                ivBackground.setImageResource(R.drawable.bg_lottery_regret);
                tvPrize.setText("再接再厉，再来一次吧！");
                tvLeft.setText("再来一次");
                tvRight.setText("稍后继续");
                break;
            case 2: // 无抽奖次数
                ivTitle.setImageResource(R.drawable.bg_lottery_shortage_title);
                ivBackground.setImageResource(R.drawable.bg_lottery_shortage);
                tvPrize.setText("分享活动至微信、QQ，\n免费得抽奖机会！");
                tvLeft.setText("去分享");
                tvRight.setText("返回");
                break;
        }


        tvLeft.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                loDialog.dismiss();
            }
        });

        tvRight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (showType == 0)
                    GotoUtil.gotoActivity(poActivity, MyPrizeListActivity.class);
                loDialog.dismiss();
            }
        });
    }

}
